package com.yejiali.backend.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yejiali.backend.common.httpcode.HttpCodeEnum;
import com.yejiali.backend.entity.Hospitals;
import com.yejiali.backend.entity.dto.HospitalDTO;
import com.yejiali.backend.entity.dto.HospitalsPageInfoDTO;
import com.yejiali.backend.entity.vo.HospitalsVO;
import com.yejiali.backend.exception.SystemException;
import com.yejiali.backend.mapper.HospitalsMapper;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.HospitalsService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (Hospitals)表服务实现类
 *
 * @author yejiali
 * @since 2025-06-05 17:03:33
 */
@Service("hospitalsService")
public class HospitalsServiceImpl extends ServiceImpl<HospitalsMapper, Hospitals> implements HospitalsService {
  /**
   * 分页查询
   * @param pageNum  页码
   * @param pageSize 每页大小
   * @return
   */
  @Override
  public Response initList(int pageNum, int pageSize) {
   //创建时间降序排序
   QueryWrapper<Hospitals> queryWrapper = new QueryWrapper<>();
   queryWrapper.orderByDesc("created_at");
   //分页查询
   Page<Hospitals> page = new Page<>(pageNum, pageSize);
   page = this.page(page, queryWrapper);
   if (page.getRecords() == null) {
    return Response.success("暂无合作医院信息");
   }
   List<Hospitals> hospitalsList = page.getRecords();
   List<HospitalsVO> hospitalsVOS = BeanUtil.copyToList(hospitalsList, HospitalsVO.class);
   //封装返回结果
   HospitalsPageInfoDTO hospitalsPageInfoDTO = new HospitalsPageInfoDTO(page.getTotal(), hospitalsVOS);
   return Response.success(hospitalsPageInfoDTO);
  }



 /**
  * 按所给条件查询
  * @param
  * @return
  */
 @Override
 public Response search(String name, String level, String type, String status, int pageNum, int pageSize) {
  QueryWrapper<Hospitals> queryWrapper = new QueryWrapper<>();
  if (name != null && !name.isEmpty()) {
   queryWrapper.like("hospital_name", name);
  }
  if (level != null && !level.isEmpty()) {
   queryWrapper.eq("hospital_grade", level);
  }
  if (type != null && !type.isEmpty()) {
   queryWrapper.eq("hospital_type", type);
  }
  if (status!= null &&!status.isEmpty()) {
   queryWrapper.eq("status", status);
  }
  Page<Hospitals> page = new Page<>(pageNum, pageSize);
  page = this.page(page, queryWrapper);
  if (page.getRecords().isEmpty()) {
   return Response.success("暂无符合条件的合作医院信息");
  }
  List<HospitalsVO> hospitalsVOS = BeanUtil.copyToList(page.getRecords(), HospitalsVO.class);
  //封装返回结果
  HospitalsPageInfoDTO hospitalsPageInfoDTO = new HospitalsPageInfoDTO(page.getTotal(), hospitalsVOS);
  return Response.success(hospitalsPageInfoDTO);
 }



 /**
  * 新增合作医院信息
  * @param hospitalDTO
  * @return
  */
 @Override
 public Response add(HospitalDTO hospitalDTO) {
  //检查是否已存在同名的合作医院
  QueryWrapper<Hospitals> queryWrapper = new QueryWrapper<>();
  queryWrapper.eq("hospital_name", hospitalDTO.getHospitalName());
  Hospitals existingHospital = this.getOne(queryWrapper);
  if (existingHospital != null) {
   throw new SystemException(HttpCodeEnum.HOSPITAL_EXIST);
  }
  //将VO对象转换为实体对象
  Hospitals hospitals = BeanUtil.copyProperties(hospitalDTO, Hospitals.class);
  //设置创建时间和更新时间
  hospitals.setCreatedAt(new Date());
  hospitals.setUpdatedAt(new Date());
  //保存到数据库
  try {
   this.save(hospitals);
  } catch (Exception e) {
   throw new SystemException(HttpCodeEnum.SYSTEM_ERROR);
  }
  return Response.success("新增合作医院信息成功");
 }

 /**
  * 删除合作医院信息
  * @param id
  * @return
  */
    @Override
    public Response delete(Integer id) {
        //根据id查询合作医院信息
        Hospitals hospitals = this.getById(id);
        if (hospitals == null) {
            throw new SystemException(HttpCodeEnum.HOSPITAL_NOT_EXIST);
        }
        //删除合作医院信息
        try {
            this.removeById(id);
        }catch (Exception e) {
            throw new SystemException(HttpCodeEnum.SYSTEM_ERROR);
        }
        return Response.success("删除合作医院信息成功");
    }


 /**
  * 修改合作医院信息
  * @param hospitals
  * @return
  */

 @Override
 public Response updateInfo(Hospitals hospitals) {
  // 检查传入的医院对象是否包含有效的 ID
  if (hospitals.getId() == null) {
   return Response.error(HttpCodeEnum.HOSPITAL_NOT_EXIST.getCode(),"未提供有效的医院 ID");
  }
  try {
   boolean success = this.updateById(hospitals);
   if (success) {
    return Response.success("医院信息更新成功");
   } else {
    return Response.error(HttpCodeEnum.SYSTEM_ERROR.getCode(), "更新失败，请检查输入数据");
   }
  } catch (Exception e) {
   // 处理更新过程中可能出现的异常
   return Response.error(HttpCodeEnum.SYSTEM_ERROR.getCode(), "更新失败，请检查输入数据");
  }
 }
}



